Skip to content

Normalize the RHS of an Unsize goal in the new solver - #113393

Merged
bors merged 5 commits into
rust-lang:masterfrom
compiler-errors:next-solver-unsize-rhs
Jul 25, 2023
Merged

Normalize the RHS of an Unsize goal in the new solver#113393
bors merged 5 commits into
rust-lang:masterfrom
compiler-errors:next-solver-unsize-rhs

Conversation

@compiler-errors

@compiler-errors compiler-errors commented Jul 6, 2023

Copy link
Copy Markdown
Contributor

Unsize goals are... tricky. Not only do they structurally match on their self type, but they're also structural on their other type parameter. I'm pretty certain that it is both incomplete and also just plain undesirable to not consider normalizing the RHS of an unsize goal. More practically, I'd like for this code to work:

trait A {}
trait B: A {}

impl A for usize {}
impl B for usize {}

trait Mirror {
    type Assoc: ?Sized;
}

impl<T: ?Sized> Mirror for T {
    type Assoc = T;
}

fn main() {
    // usize: Unsize<dyn B>
    let x = Box::new(1usize) as Box<<dyn B as Mirror>::Assoc>;
    // dyn A: Unsize<dyn B>
    let y = x as Box<<dyn A as Mirror>::Assoc>;
}

In order to achieve this, we add EvalCtxt::normalize_non_self_ty (naming modulo bikeshedding), which must be used for all non-self type arguments that are structurally matched in candidate assembly. Currently this is only necessary for Unsize's argument, but I could see future traits requiring this (hopefully rarely) in the future. It uses repeat_while_none to limit infinite looping, and normalizes the self type until it is no longer an alias.

Also, we need to fix feature gate detection for trait_upcasting and unsized_tuple_coercion when HIR typeck has unnormalized types. We can do that by checking the ImplSource returned by selection, which necessitates adding a new impl source for tuple upcasting.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants